package com.content.management.config;

import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xinghu
 * @version 1.0
 * @date 2020/2/10 1:20 上午
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.content.management.dao"})
public class MybatisConfig {


	/**
	 * 分页插件，自动识别数据库类型
	 */
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor page = new PaginationInterceptor();
		List<ISqlParser> sqlParserList = new ArrayList<>();
		// 阻止恶意的全表更新删除
		sqlParserList.add(new BlockAttackSqlParser());
		page.setSqlParserList(sqlParserList);
		// 一次最多3001
		page.setLimit(3001);
		return page;
	}

	/**
	 * 乐观锁插件
	 *
	 * @return Interceptor
	 */
	@Bean
	public OptimisticLockerInterceptor optimisticLockerInterceptor() {
		return new OptimisticLockerInterceptor();
	}
}
